"""
mcp调用
"""

from retry import retry
from fastmcp import Client
from json_repair import repair_json


async def list_tools(sse_url):
    """获取工具列表"""
    async with Client(sse_url) as client:
        tools = await client.list_tools()
        return tools


@retry(tries=3, delay=1)
async def call_tool(sse_url, tool_name, tool_args):
    """调用工具"""
    async with Client(sse_url) as client:
        result = await client.call_tool(
            name = tool_name, arguments = tool_args
        )
        if not isinstance(result.content, list) or len(result.content) == 0:
            return None
        text = result.content[0].text
        data = repair_json(text, return_objects=True)
        return data
